var data;
var totalPrice;
$.ajax({
  url: "/shop/list",
  async: false,
  success: function(res) {
    for (var type in res.result) {
      res.result[type].type = type;
      var html = template("item", res.result[type]);
      if (type == "场地") {
        $("#siteList").append(html);
      }
      if (type == "场地布置") {
        $("#layoutList").append(html);
      }
      if (type == "餐饮服务") {
        $("#goodsList").append(html);
      }
      if (type == "达人服务") {
        $("#personList").append(html);
      }
      if (type == "包车服务") {
        $("#carList").append(html);
      }
    }

    mui(".cj-item-opt").on("tap", ".deleteItem", function() {
      var btnArray = ['否', '是'];
      var id = this.getAttribute("data-id");
      mui.confirm('您确定要删除该订单？', '确认', btnArray, function(e) {
        if (e.index == 1) {
          $.ajax({
            url: '/shop/delete/'+id,
            dataType: 'json',
            success: function(response) {
              if (response.code == 200) {
                location.reload();
              }
            }
          });

        }
      });
    });

    mui(".cj-num").on("change",".mui-input-numbox",function(){
        var id = this.getAttribute("data-id");
        var value = this.value;
        $.ajax({
          url: '/shop/update?spid='+id+'&number='+value,
          dataType: 'json',
          success: function(response) {

          }
        });
    });

  }
});


mui(".mui-scroll-wrapper").scroll({
  indicators: true //是否显示滚动条
});

var item = $(".cj-car-item");
var totalPriceDom = $("#totalPrice");

$(".cj-car-list").each(function() {
  if ($(this).find(".cj-car-item").length > 0) {
    $(this).find(".cj-no-goods").hide();
  }
});

$(".cj-car-item input[type='checkbox']").change(function() {
  compute();
});

$(".mui-input-numbox").change(function() {
  compute();
});

$("input[name='all']").click(function() {
  var all = $(this);
  var checked = all.get(0).checked;
  // console.log(checked);
  if (checked) {
    $(".cj-car-item-select input[type='checkbox']").prop('checked', true);
  } else {
    $(".cj-car-item-select input[type='checkbox']").prop('checked', false);
  }
  compute();
});

$("#balance").click(function() {

    var price = new Array();
        price['包车服务'] = 0;
        price['场地'] = 0;
        price['场地布置'] = 0;
        price['达人服务'] = 0
        price['餐饮服务'] = 0;
    var orderNum = new Array();
    item.each(function() {
      var self = $(this);
      var orderdom = self.find("input[type='checkbox']").get(0);
      if (orderdom.checked) {
        orderNum.push(orderdom.value);
        var prices = self.find(".price").text();
        var num = self.find("input[type='number']").val();
        price[orderdom.getAttribute("data-type")] += parseFloat(prices) * parseFloat(num);
        //console.log(num);
      }
    });

    var ordersNum = orderNum.join("|");
    //  console.log(ordersNum+"--"+totalPrice);
    //  console.log(price);
     if(ordersNum=="" || ordersNum==null){
       mui.toast("请选择商品");
       return ;
     }
     location.href = "/orders?ordersNum="+ordersNum+"&price="+totalPrice+"&car="+price['包车服务']+"&site="+price['场地']+"&layout="+price['场地布置']+"&person="+price['达人服务']+"&catering="+price['餐饮服务'];

});

function compute() {
   totalPrice = 0;
  item.each(function() {
    var self = $(this);
    if (self.find("input[type='checkbox']").get(0).checked) {
      var price = self.find(".price").text();
      var num = self.find("input[type='number']").val();
      totalPrice += parseFloat(price) * parseFloat(num);
      //console.log(num);
    }
  });
  totalPrice = totalPrice.toFixed(2);
  totalPriceDom.text(totalPrice );
}
